
local mt = ac.buff['自动施法']
mt.keep = true
mt.pulse = 1

function mt:on_pulse()
    local unit = self.target
    local player = unit.owner
    if not player:get_data('开关-自动施法') then
        return
    end
    if not unit:is_alive() then
        return
    end
    if unit:has_restriction '硬直' then
        return
    end
    for skill in unit:each_skill('英雄', true) do 
        if not skill.is_order and not skill:is_passive() then
            local target = ac.selector('unit'):range(unit,math.max(skill.range,skill.area or 0)):enemy(unit):random()
            if target and skill:cast_check() then
                if skill.targettype == '没有' then
                    skill:cast()
                elseif skill.targettype == '物体' then
                    skill:cast(target)
                elseif skill.targettype == '点' then
                    skill:cast(target:get_point())
                end
                return
            end
        end
    end 
end

ac.game:event '玩家-选择英雄完毕'(function(_,player,hero)
    hero:add_buff '自动施法'{keep = true}
end)

local mt = ac.buff['怪物-自动施法']
mt.keep = true
mt.pulse = 0.5

function mt:on_pulse()
    local unit = self.target
    local player = unit.owner
    if not unit:is_alive() then
        return
    end
    if unit:has_restriction '硬直' then
        return
    end
    for skill in unit:each_skill('英雄', true) do 
        if not skill.is_order and not skill:is_passive() then
            local target = ac.selector('unit'):range(unit,math.max(skill.range,skill.area or 0)):enemy(unit):is_not(ac.base_unit):random()
            if target and skill:cast_check() then
                if skill.targettype == '没有' then
                    skill:cast()
                elseif skill.targettype == '物体' then
                    skill:cast(target)
                elseif skill.targettype == '点' then
                    skill:cast(target:get_point())
                end
                return
            end
        end
    end 
end
